package constant.milk.cashbook.photo;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.data.DataMainActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.util.MilkUtil;

public class PhotoActivity extends SuperActivity
{
	public static final String PHOTH_PASH = "PHOTH_PASH";

	private static final int PHOTO_REQUEST_CODE = 0;
	private static final int VIEW_REQUEST_CODE = 1;

	private ImageView photoImageView;

	private Button deleteButton;

	private Button checkButton;

	private String path;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.photo_activity);

		setTitle("사진");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		photoImageView = (ImageView) findViewById(R.id.photoImageView);

		deleteButton = (Button) findViewById(R.id.deleteButton);
		checkButton = (Button) findViewById(R.id.checkButton);
	}

	@Override
	protected void setOnClick()
	{
		photoImageView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				startCamera();
			}
		});

		deleteButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (path != null)
				{
					try
					{
						File file = new File(path);

						file.delete();

					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}

				MilkUtil.showToast(superContext, "사진이 삭제 되었습니다.");
				path = "";

				photoImageView.setImageBitmap(null);

				deleteButton.setVisibility(View.GONE);
			}
		});

		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(PHOTH_PASH, path);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		Intent intent = getIntent();
		String photoPath = intent.getStringExtra(PHOTH_PASH);

		path = "";

		if (photoPath.equals(""))
		{
			deleteButton.setVisibility(View.GONE);
		} else
		{
			deleteButton.setVisibility(View.VISIBLE);

			Bitmap bitmap = BitmapFactory.decodeFile(photoPath);

			if (bitmap == null)
			{
				MilkUtil.showToast(superContext, "사진을 찾을 수 없습니다.");
				photoImageView.setImageBitmap(null);
				path = "";
			} 
			else
			{
				photoImageView.setImageBitmap(bitmap);
				photoImageView.setScaleType(ImageView.ScaleType.FIT_XY);
				path = photoPath;
			}

		}
	}

	@Override
	public void onBackPressed()
	{
		Intent intent = new Intent();
		intent.putExtra(PHOTH_PASH, path);
		setResult(Activity.RESULT_OK, intent);

		super.onBackPressed();
	}

	private void startCamera()
	{
		if (path.equals(""))
		{
			Intent intent = new Intent(superContext, CameraActivity.class);
			startActivityForResult(intent, PHOTO_REQUEST_CODE);
		} 
		else
		{
			File file = new File(path);
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(
					Uri.parse("file://" + file.getAbsolutePath()), "image/*");
			startActivityForResult(intent, VIEW_REQUEST_CODE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}

		if (requestCode == PHOTO_REQUEST_CODE)
		{
			path = data.getStringExtra(CameraActivity.PASH);

			Bitmap bitmap = BitmapFactory.decodeFile(path);

			photoImageView.setImageBitmap(bitmap);
			photoImageView.setScaleType(ImageView.ScaleType.FIT_XY);

			deleteButton.setVisibility(View.VISIBLE);
		} 
		else if (requestCode == VIEW_REQUEST_CODE)
		{
			Bitmap bitmap = BitmapFactory.decodeFile(path);

			if (bitmap == null)
			{
				photoImageView.setImageBitmap(null);

				deleteButton.setVisibility(View.GONE);
			}
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_PHOTO);
		startActivity(intent);
	}

}
